package top.drinyeit.nongmao.presenter;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import top.drinyeit.nongmao.activity.SecondCategoryNewActivity;
import top.drinyeit.nongmao.adapter.CVAdapter;
import top.drinyeit.nongmao.adapter.HomeClassfyAdapter;
import top.drinyeit.nongmao.adapter.HomeIconAdapter;
import top.drinyeit.nongmao.adapter.HomeMoreAdapter;
import top.drinyeit.nongmao.fragment.HomeFragment;
import top.drinyeit.nongmao.model.AppHomeBean;
import top.drinyeit.nongmao.model.CategoryGoodsDetailsBean;
import top.drinyeit.nongmao.retrofit.HttpResponse;
import top.drinyeit.nongmao.retrofit.RetrofitMethod;
import top.drinyeit.nongmao.retrofit.subscriber.CommonSubscriber;
import top.drinyeit.nongmao.retrofit.subscriber.SubscriberListener;

import static top.drinyeit.nongmao.app.Constant.COMMON_SUCCESS;

/**
 * Created by wu on 2018/1/8.
 */

public class HomePresenter {
    private HomeFragment fragment;
    private List<AppHomeBean.BannerBean> banner;
    private CVAdapter cvAdapter;
    private HomeMoreAdapter homeMoreAdapter;
    private HomeIconAdapter homeIconAdapter;

    public HomePresenter(HomeFragment fragment) {
        this.fragment = fragment;
    }

    //获取首页信息
    public void getApp() {
        banner = new ArrayList<>();
        CommonSubscriber<HttpResponse<AppHomeBean>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<AppHomeBean>>() {
            @Override
            public void onError(String e, int code) {
                fragment.swipeRefreshLayout.finishRefresh();
                fragment.showToastMsg(e);
            }

            @Override
            public void onNext(final HttpResponse<AppHomeBean> o) {
                fragment.swipeRefreshLayout.finishRefresh();
                if (o.getCode() != COMMON_SUCCESS) {
                    fragment.showToastMsg(o.getMessage());
                } else {
                    int c = o.getData().getCommodity().size();
                    final int b = o.getData().getBanner().size();
                    int ca = o.getData().getCategory().size();
                    //设置底部广告
                    homeMoreAdapter = new HomeMoreAdapter(o.getData().getCommodity());
                    List<CategoryGoodsDetailsBean> array = new ArrayList<>();
                    HomeClassfyAdapter homeClassfyAdapter=new HomeClassfyAdapter(fragment.getActivity(),array);
                    fragment.moreRecyclerView.setLayoutManager(new LinearLayoutManager(fragment.getActivity()));
                    fragment.moreRecyclerView.setAdapter(homeClassfyAdapter);
                    fragment.moreRecyclerView.setNestedScrollingEnabled(true);

                    //设置分类
                    if (homeIconAdapter == null) {
                        homeIconAdapter = new HomeIconAdapter(o.getData().getCategory());
                        fragment.iconRecycle.setLayoutManager(new GridLayoutManager(fragment.getActivity(), 4));
                        homeIconAdapter.setOnItemClickListener(new HomeIconAdapter.OnItemClickListener() {
                            @Override
                            public void onItemClick(View view, int position) {
                                Intent intent = new Intent(fragment.getActivity(), SecondCategoryNewActivity.class);
                                intent.putExtra("category", o.getData().getCategory().get(position));
                                intent.putExtra("type","home");
                                fragment.startActivity(intent);
                            }
                        });
                        fragment.iconRecycle.setAdapter(homeIconAdapter);
                        fragment.iconRecycle.setNestedScrollingEnabled(false);
                    }

                    if (cvAdapter == null) {
                        banner = o.getData().getBanner();
                        cvAdapter = new CVAdapter(fragment.getActivity(), banner);
                        fragment.getCarouselView().setAdapter(cvAdapter);
                        fragment.getCarouselView().setRoundDotVisible(false);
                    }

                }
            }
        });
        RetrofitMethod.getInstance().getApp(subscriber);
    }
}
